﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class Pages_Admin_AdaugareProdus : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //fac text-area care sa ruleze la server :D
        ShortDescription_tb.TextMode = TextBoxMode.MultiLine;
        DetailedDescription_tb.TextMode = TextBoxMode.MultiLine;

        Mesaj_success_panel.Visible = false;
        Mesaj_err_DB_panel.Visible = false;
        Mesaj_err_insert_panel.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection connection = new SqlConnection(GetConnectionString());      //deschid conexiune catre baza de date
            SqlCommand command = new SqlCommand("Products_Insert", connection);          //invoc procedura stocata
            command.CommandType = CommandType.StoredProcedure;                        //setez ca o sa chem o procedura stocata

            /*
             * 	  @ID int,
                  @BrandID int,
                  @CategoryID int,
                  @name nvarchar(50),
                  @price money,
                  @old_price money,
                  @short_description nvarchar(140),
                  @full_description text,
                  @code nvarchar(45),
                  @supplier_stock int,
                  @is_active bit
             * */


            command.Parameters.AddWithValue("@ID", "0");          //nu conteaza ce valoare are

            command.Parameters.AddWithValue("@BrandID", Brand_ddl.SelectedValue);

            command.Parameters.AddWithValue("@CategoryID", Categorie_ddl.SelectedValue);

            command.Parameters.AddWithValue("@name", ProductName_tb.Text);

            command.Parameters.AddWithValue("@price", Pret_tb.Text);

            command.Parameters.AddWithValue("@old_price", 0);

            command.Parameters.AddWithValue("@short_description", ShortDescription_tb.Text);

            command.Parameters.AddWithValue("@full_description", DetailedDescription_tb.Text);

            command.Parameters.AddWithValue("@code", CodProdus_tb.Text);

            command.Parameters.AddWithValue("@supplier_stock", StocFurnizor_tb.Text);

            command.Parameters.AddWithValue("@shop_stock", StocMagazin_tb.Text);

            command.Parameters.AddWithValue("@is_active", Disponibilitate_cb.Checked);
            command.Parameters.AddWithValue("@is_promotion", Promotie_cb.Checked);

            var serverDirectory = "~/Content/ProductsImages/";
            var rootpath = Server.MapPath(serverDirectory);

            var fileName = Poza1.PostedFile.FileName;
            fileName =  System.IO.Path.GetFileName(fileName);
            Poza1.PostedFile.SaveAs(rootpath + fileName);
            command.Parameters.AddWithValue("@image_url1", serverDirectory + fileName);

            fileName = Poza2.PostedFile.FileName;
            if (!String.IsNullOrEmpty(fileName))
            {
                fileName = System.IO.Path.GetFileName(fileName);
                Poza2.PostedFile.SaveAs(rootpath + fileName);
                command.Parameters.AddWithValue("@image_url2", serverDirectory + fileName);
            }
            fileName = Poza3.PostedFile.FileName;
            if (!String.IsNullOrEmpty(fileName))
            {

                fileName = System.IO.Path.GetFileName(fileName);
                Poza3.PostedFile.SaveAs(rootpath + fileName);
                command.Parameters.AddWithValue("@image_url3", serverDirectory + fileName);
            }
            fileName = Poza4.PostedFile.FileName;
            if (!String.IsNullOrEmpty(fileName))
            {
                
                fileName = System.IO.Path.GetFileName(fileName);
                Poza4.PostedFile.SaveAs(rootpath + fileName);
                command.Parameters.AddWithValue("@image_url4", serverDirectory + fileName);
            }

            connection.Open();
            int nr_insert = command.ExecuteNonQuery();      //numarul de inserturi in baza de date

            if (nr_insert == -1 || nr_insert == 1)
                Mesaj_err_insert_panel.Visible = true;
            else
                Mesaj_success_panel.Visible = true;          //adica am avut 2 inserturi(Produse si Imagini)
        }
        catch (Exception E)
        {
            Mesaj_err_DB_panel.Visible = true;
        }
    }

    #region "DBA"
    string GetConnectionString(string connection_name = "StoreDBConnectionString")
    {
        string connection = string.Empty;

        connection = ConfigurationManager.ConnectionStrings[connection_name].ConnectionString;

        return connection;
    }
    #endregion
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Button1_Click(sender, e);
    }
}